package com.zytech.zspider.core;

import com.google.common.base.Objects;
import com.zytech.modules.lang.Strings;

import java.io.Serializable;

/**
 * @author siuming
 */
public final class TaskResultPipelineSettings implements Serializable {
    private static final long serialVersionUID = 4902290095445039290L;

    private final String retryWhen;
    private final String process;
    private final int retryTimes;

    TaskResultPipelineSettings(Builder builder) {
        this.retryWhen = builder.retry;
        this.process = builder.process;
        this.retryTimes = builder.retryTimes;
    }

    /**
     * @return
     */
    public boolean isRequiresRetry() {
        return !Strings.isBlank(retryWhen);
    }

    /**
     * @return
     */
    public String getRetryWhen() {
        return retryWhen;
    }

    /**
     * @return
     */
    public boolean isRequiresProcess() {
        return !Strings.isBlank(process);
    }

    /**
     * @return
     */
    public String getProcess() {
        return process;
    }

    /**
     * @return
     */
    public int getRetryTimes() {
        return retryTimes;
    }

    @Override
    public String toString() {
        return Objects
                .toStringHelper(this)
                .add("retryWhen", getRetryWhen())
                .add("retryTimes", getRetryTimes())
                .add("process", getProcess())
                .toString()
                ;

    }

    /**
     * @return
     */
    public static Builder ofBuilder() {
        return new Builder();
    }

    public static class Builder {
        int retryTimes;
        String retry;
        String process;

        Builder() {
        }

        /**
         * @param retry
         */
        public Builder retryWhen(String retry) {
            this.retry = retry;
            return this;
        }

        public Builder retryTimes(int retryTimes) {
            this.retryTimes = retryTimes;
            return this;
        }

        public Builder process(String reprocess) {
            this.process = reprocess;
            return this;
        }

        public TaskResultPipelineSettings build() {
            return new TaskResultPipelineSettings(this);
        }
    }
}
